//
//  JHRegistVC.m
//  XMPPChat
//
//  Created by Boss Lin on 16/5/23.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "JHRegistVC.h"

@interface JHRegistVC ()

@property (weak, nonatomic) IBOutlet UITextField *usernameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UITextField *confirmField;

@end


@implementation JHRegistVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registResult:) name:kREGIST_RESULT object:nil];
}

- (IBAction)registAction:(id)sender {
    NSString *username = _usernameField.text;
    NSString *password = _passwordField.text;
    NSString *confirm = _confirmField.text;
    
    NSString *message = nil;
    if (username.length <= 0) {
        message = @"用户名未填写";
    } else if (password.length <= 0) {
        message = @"密码未填写";
    } else if (confirm.length <= 0) {
        message = @"确认密码未填写";
    }
    
    if (message.length > 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
        [alertView show];
    } else if (![password isEqualToString:confirm]) {
        message = @"密码与确认密码不一致";
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
        [alertView show];
    } else {
        [[CZXMPPManager shareInstance] registerWithJID:[XMPPJID jidWithUser:username domain:kCZ_DOMAIN resource:kCZ_RESOURCE] andPassword:password];
    }}

- (IBAction)cancelClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - notification event
- (void)registResult:(NSNotification *)notification
{
    NSNumber *number = notification.object;
    NSString *message = @"";
    if (number.boolValue) {
        message = @"注册成功";
    } else {
        message = @"注册失败";
    }
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
    [alertView show];
}



@end
